package com.skyc.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.rowset.CachedRowSet;

import com.skyc.dao.OrderDao;
import com.skyc.entity.Order;
import com.skyc.util.JDBCUtil;

public class OrderDaoImpl implements  OrderDao {
	JDBCUtil util=JDBCUtil.getJDBCUtil();
	@Override
	public List<Order> queryByUid(String uid) {
		String sql="select * from t_order where u_id=? order by o_date desc";
		CachedRowSet crs  =util.query(sql,uid);
		return returnlist(crs);
		
	}
	
	private  List<Order> returnlist(CachedRowSet crs) {
		List<Order> list= new ArrayList<Order>();
	try {
		while(crs.next()) {
			ClothesDaoImpl cdao= new ClothesDaoImpl();
			Order o=new Order();
			o.setOid(crs.getInt("o_id"));
			o.setUid((crs.getInt("u_id")));
			o.setOprice(crs.getInt("o_price"));
			o.setOdate(crs.getString("o_date"));
			o.setOstate(crs.getInt("o_state"));
			o.setOsend(crs.getInt("o_send"));
			o.setOtuihuo(crs.getInt("o_tuihuo"));
			o.setOpayed(crs.getInt("o_payed"));
			o.setOnum(crs.getInt("o_num"));
			o.setOsuccess(crs.getInt("o_success"));		
			o.setClothes(cdao.queryByID(crs.getInt("c_id")+""));
			list.add(o);			
		}		
	} catch (Exception e) {
		e.printStackTrace();
	}finally {
		util.closeCacheRowSet(crs);
	}
	return list;
	}

	@Override
	public boolean updateOpayedByOid(String oid) {
		String sql="update t_order set o_payed=1 where o_id=?";
		return  util.update(sql, oid);
	}

	@Override
	public boolean updateOsendByOid(String oid) {
		String sql="update t_order set o_send=1 where o_id=?";
		return  util.update(sql, oid);
	}

	@Override
	public boolean updateOstateByOid(String oid) {
		String sql="update t_order set o_state=1 where o_id=?";
		return  util.update(sql, oid);
	}

	@Override
	public boolean deleteByOid(String oid) {
		String  sql="delete  from t_order  where o_id=?";
		return  util.update(sql, oid);
	}

	@Override
	public Order queryByOid(String oid) {
		String sql="select * from  t_order where o_id=?";
		CachedRowSet crs=util.query(sql, oid);
		Order o=new Order();
		try {
			if(crs.next()) {
				ClothesDaoImpl cdao= new ClothesDaoImpl();
				o.setOid(crs.getInt("o_id"));
				o.setUid((crs.getInt("u_id")));
				o.setOprice(crs.getInt("o_price"));
				o.setOdate(crs.getString("o_date"));
				o.setOstate(crs.getInt("o_state"));
				o.setOsend(crs.getInt("o_send"));
				o.setOtuihuo(crs.getInt("o_tuihuo"));
				o.setOpayed(crs.getInt("o_payed"));
				o.setOnum(crs.getInt("o_num"));
				o.setOsuccess(crs.getInt("o_success"));		
				o.setClothes(cdao.queryByID(crs.getInt("c_id")+""));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			util.closeCacheRowSet(crs);
		}
		return o;
	}

	@Override
	public boolean updateOtuihuoByOid(String oid,String num) {
		String sql="update t_order set o_tuihuo=? where o_id=?";
		return  util.update(sql, num,oid);
	}

	@Override
	public boolean addOrder(Order order) {
		String sql="INSERT into t_order (u_id,o_price, o_num,c_id, o_state,o_send,o_tuihuo,o_payed,o_success,o_order) VALUES(?,?,?,?,?,?,?,?,?,?)";
		return util.update(sql,order.getUid(),order.getOprice(),order.getOnum(),order.getClothes().getCid(),order.getOstate(),order.getOsend(),order.getOtuihuo(),order.getOpayed(),order.getOsuccess(),order.getOorder());
			
	}

	@Override
	public List<Integer> queryfirstoid(int num) {
		String sql="select o_id from t_order order by o_id desc limit  "+num;
		System.out.println(sql);
		CachedRowSet crs=util.query(sql);
		List<Integer> oid=new ArrayList<Integer>();
		try {
			while(crs.next()) {
				oid.add(crs.getInt("o_id"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			util.closeCacheRowSet(crs);
		}
     	return oid;
	}
}
